#include "data.h"
#include "bmp.h"
#include "font.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <iconv.h>

// 注册宏
#define GEC6818_GET_DHTDATA _IOR('K', 0, unsigned int)
#define GEC6818_ADC_IN0 _IOR('A', 1, unsigned long)

#define LED0_OFF _IO('x', 0)
#define LED1_OFF _IO('x', 1)
#define LED2_OFF _IO('x', 2)
#define LED3_OFF _IO('x', 3)

#define GEC6818_BEEP_ON _IO('B', 0)
#define GEC6818_BEEP_OFF _IO('B', 1)

struct sockaddr_in server_addr;

int mysend(int com, int a, int b, int c, int d)
{
    if (-1 == network_control)
    {
        return -1;
    }
    char buf[128];
    memset(buf, 0, sizeof(buf));
    sprintf(buf, "%d,%d,%d,%d,%d", com, a, b, c, d);
    int ret = write(fd_socket, buf, sizeof(buf));
    // switch (com)
    // {
    // case 1:
    //     printf("1 湿度= %d  温度=%d 光照强度= %d 可燃气浓度= %d\n", a, b, c, d);
    //     break;
    // case 2:
    //     printf("2 初始化状态请求\n");
    //     break;
    // case 3:
    //     printf("3 湿度开关= %d  湿度开关=%d 温度开开关= %d 可燃气体开关= %d\n", a, b, c, d);
    //     break;
    // case 4:
    //     printf("4 智能调节控制开关= %d\n", a);
    //     break;
    // default:
    //     break;
    // }

    return ret;
}

void data_init(void)
{
    fd_beep = open("/dev/beep_drv", O_WRONLY);
    if (fd_beep < 0)
    {
        perror("fd_beep open fail");
    }
    fd_dth11 = open("/dev/dht11_dev", O_WRONLY);
    if (fd_dth11 < 0)
    {
        perror("fd_dth11 open fail");
    }
    fd_light = open("/dev/light_drv", O_RDWR);
    if (fd_light < 0)
    {
        perror("fd_light open fail");
    }
    fd_adc = open("/dev/adc_drv", O_WRONLY);
    if (fd_adc < 0)
    {
        perror("fd_adc open fail");
    }
    fd_led = open("/dev/Led", O_WRONLY);
    if (fd_led < 0)
    {
        perror("fd_led open fail");
    }
    wd = 0;
    sd = 0;
    gz = 0;
    qt = 0;
    _switch[0] = 0;
    _switch[1] = 0;
    _switch[2] = 0;
    _switch[3] = 0;
    bzwd = 20;
    bzsd = 50;
    bzgz = 50;
    bzqt = 0;

    adjust_control = 0;
    network_control = 0;

    adjust_cmd = 0;
    network_cmd = 0;

    ioctl(fd_led, LED0_OFF, 1); // 光照 1 为关
    ioctl(fd_led, LED1_OFF, 1); // 湿度
    ioctl(fd_led, LED2_OFF, 1); // 温度
    ioctl(fd_led, LED3_OFF, 1); // 通风
    return;
}
int beepOn = 0;
void *been_on_off(void *a)
{
    ioctl(fd_led, LED3_OFF, 0); // 通风
    _switch[3] = 1;
    while (beepOn)
    {
        ioctl(fd_beep, GEC6818_BEEP_ON);
        usleep(300 * 1000);
        ioctl(fd_beep, GEC6818_BEEP_OFF);
        usleep(300 * 1000);
    }
    ioctl(fd_led, LED3_OFF, 1); // 通风
    _switch[3] = 0;
    return NULL;
}

void *data_send(void *arg)
{
    unsigned char data[2];
    int ret;
    char buf[123] = {0};
    pthread_t thread;
    while (1)
    {
        // 读取稳定湿度
        ioctl(fd_dth11, GEC6818_GET_DHTDATA, &data[0]);
        // 读取ADC通道0的电压 - 读取光照强度值
        ioctl(fd_adc, GEC6818_ADC_IN0, &gz);
        // 读取可燃气体
        read(fd_light, &qt, 4);

        sd = data[1];
        wd = data[0];
        gz = 100 - ((gz - 400) / 14);

        if (qt == 1)
        {
            // ioctl(fd_beep, GEC6818_BEEP_ON);
            // _switch[3] = 1;
            // ioctl(fd_led, LED3_OFF, 0); // 通风
            if (beepOn == 0)
            {
                beepOn = 1;
                pthread_create(&thread, NULL, been_on_off, NULL);
                pthread_detach(thread);
            }
        }
        else
        {
            beepOn = 0;
            // ioctl(fd_beep, GEC6818_BEEP_OFF);
        }

        if (-1 != network_control && wd < 50 && wd > 0 && sd < 100 && sd > 0 && 0 < gz && gz < 100)
        {
            ret = mysend(1, wd, sd, gz, qt);
            if (ret <= 0 && network_control != -1)
            {
                perror("fd_socket write fail");
                network_control = -1;
                if (network_cmd == 1)
                {
                    wl_cmd(s);
                }
            }
        }
        else if (-1 == network_control)
        {
            socket_init(server_ip);
            socket_connect();
            if (network_control != -1)
            {
                pthread_create(&thread_read, NULL, data_read, NULL);
                pthread_detach(thread_read);
            }
        }
        usleep(1000 * 2000);
    }
    return NULL;
}

void *data_read(void *arg)
{
    char buf[128] = {0};
    int ret = 1;
    int com, a1, a2, a3, a4;
    while (network_control != -1)
    {
        memset(buf, 0, sizeof(buf));
        ret = read(fd_socket, buf, sizeof(buf));
        if (ret <= 0)
        {
            perror("fd_socket read fail");
            network_control = -1;
            if (network_cmd == 1)
            {
                wl_cmd(s);
            }
            return NULL;
        }
        sscanf(buf, "%d,%d,%d,%d,%d", &com, &a1, &a2, &a3, &a4);

        // printf("data_read:%s\n", buf);

        switch (com)
        {
        case 1:
            if (a1 == -1)
            {
                adjust_control = -1;
            }
            else
            {
                adjust_control = 0;
                bzwd = a1;
                bzsd = a2;
                bzgz = a3;
                bzqt = a4;
            }
            if (adjust_cmd == 1)
            {
                bz_cmd(s);
            }
            break;
        case 2:
            if (_switch[0] != a1 ||
                _switch[1] != a2 ||
                _switch[2] != a3 ||
                _switch[3] != a4)
                adjust_control = -1;
            _switch[0] = a1;
            _switch[1] = a2;
            _switch[2] = a3;
            _switch[3] = a4;
            break;
        default:
            break;
        }
    }
    return NULL;
}

void *data_adjust(void *arg)
{
    char buf[][7] = {"/5.bmp", "/4.bmp"};
    // 6 网络连接图标
    char wl_buf[][7] = {"/6.bmp", "/7.bmp"};
    char zn_buf[][7] = {"/9.bmp", "/8.bmp"};
    char buf1[][8] = {"/11.bmp", "/10.bmp"};
    int a = 0, b = 0, c = 0, d = 0, e = 0;
    while (1)
    {
        a = 0, b = 0, c = 0, d = 0;
        bmp_display(s, 700, 0, wl_buf[!network_control]);
        bmp_display(s, 750, 0, zn_buf[!adjust_control]);
        if (e != adjust_control)
        {
            e = adjust_control;
            mysend(4, adjust_control, 0, 0, 0);
            if (adjust_cmd == 1)
            {
                bz_cmd(s);
            }
        }

        if (-1 != adjust_control)
        {
            bmp_display(s, 750, 0, "/8.bmp");
            if (wd < bzwd || ((wd - bzwd) > 5))
            {
                c = 1;
            }
            if (sd < bzsd)
            {
                b = 1;
            }
            if (gz < bzgz)
            {
                a = 1;
            }
            if (qt >= bzgz)
            {
                d = 1;
                ioctl(fd_beep, GEC6818_BEEP_ON);
            }
            if (_switch[0] != a || _switch[1] != b || _switch[2] != c || _switch[3] != d)
            {
                _switch[0] = a;
                _switch[1] = b;
                _switch[2] = c;
                _switch[3] = d;
                mysend(3, _switch[2], _switch[1], _switch[0], _switch[3]);
            }
        }
        if (2 == myindex)
        {
            for (int i = 0; i < 4; ++i)
            {
                bmp_display(s, 440, 98 + i * 70, buf[_switch[i]]);
            }
        }

        ioctl(fd_led, LED0_OFF, !_switch[0]); // 光照
        ioctl(fd_led, LED1_OFF, !_switch[1]); // 湿度
        ioctl(fd_led, LED2_OFF, !_switch[2]); // 温度
        ioctl(fd_led, LED3_OFF, !_switch[3]); // 通风
        usleep(1000);
    }
    return NULL;
}

void *data_myprint(void *arg)
{
    char buf[4][128];
    lcd *s = (lcd *)arg;
    while (1)
    {
        for (int i = 0; i < 4; ++i)
        {
            memset(buf[i], 0, sizeof(buf[i]));
        }
        sprintf(buf[0], "%d", wd);
        sprintf(buf[1], "%d", sd);
        sprintf(buf[2], "%d", gz);
        sprintf(buf[3], "%d", qt);
        bmp_display(s, 0, 0, "/2.bmp");
        Display_characterX(385, 225, buf[0], 0xffffff, 3); // 温度
        Display_characterX(385, 365, buf[1], 0xffffff, 3); // 湿度
        Display_characterX(650, 105, buf[2], 0xffffff, 3); // 光照
        Display_characterX(240, 115, buf[3], 0xffffff, 3); // 气体
        usleep(1000 * 2000);
    }
    return NULL;
}
void *device_mycontrol(void *arg)
{
    // 0关 1开
    lcd *s = (lcd *)arg;
    char buf[][7] = {"/5.bmp", "/4.bmp"};
    bmp_display(s, 0, 0, "/3.bmp");
    diao touch_s;
    touch_s.touch_diao_x = 0;
    touch_s.touch_diao_y = 0;
    while (1)
    {
        for (int i = 0; i < 4; ++i)
        {
            bmp_display(s, 440, 98 + i * 70, buf[_switch[i]]);
        }
        dan_dian(&touch_s);
        if (touch_s.touch_diao_x >= 555 && touch_s.touch_diao_x <= 665)
        {
            if (touch_s.touch_diao_y >= 100 && touch_s.touch_diao_y <= 190)
            {
                adjust_control = -1;
                _switch[0] = (_switch[0] + 1) % 2;
            }
            else if (touch_s.touch_diao_y >= 210 && touch_s.touch_diao_y <= 280)
            {
                adjust_control = -1;
                _switch[1] = (_switch[1] + 1) % 2;
            }
            else if (touch_s.touch_diao_y >= 300 && touch_s.touch_diao_y <= 370)
            {
                adjust_control = -1;
                _switch[2] = (_switch[2] + 1) % 2;
            }
            else if (touch_s.touch_diao_y >= 390 && touch_s.touch_diao_y <= 450)
            {
                adjust_control = -1;
                _switch[3] = (_switch[3] + 1) % 2;
            }
        }

        mysend(3, _switch[2], _switch[1], _switch[0], _switch[3]);

        ioctl(fd_led, LED0_OFF, !_switch[0]); // 光照
        ioctl(fd_led, LED1_OFF, !_switch[1]); // 湿度
        ioctl(fd_led, LED2_OFF, !_switch[2]); // 温度
        ioctl(fd_led, LED3_OFF, !_switch[3]); // 通风
    }
    return NULL;
}

int socket_init(char *addr)
{
    // 创建sock
    fd_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (fd_socket < 0)
    {
        perror("create socket failed");
        return -1;
    }

    // 初始化struct sockaddr_in server_addr
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(8888);
    server_addr.sin_addr.s_addr = inet_addr(addr);

    return 0;
}

int socket_connect(void)
{
    // 链接服务端
    network_control = connect(fd_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
    if (network_control == -1)
    {
        // perror("连接不到服务器 : ");
        return -1;
    }
    else
    {
        usleep(1000);
        char buf[128] = {0};
        mysend(2, 0, 0, 0, 0);
        read(fd_socket, buf, sizeof(buf));
        sscanf(buf, "%d,%d,%d,%d,%d,%d,%d,%d,%d", &adjust_control, &bzwd, &bzsd, &bzgz, &bzqt, &_switch[0], &_switch[1], &_switch[2], &_switch[3]);
    }
    return 0;
}

void bz_cmd(lcd *s)
{
    char buf1[][8] = {"/11.bmp", "/10.bmp"};
    bmp_display(s, 600, 35, buf1[!adjust_control]);
    if (-1 != adjust_control)
    {
        char buf[4][128];
        for (int i = 0; i < 4; ++i)
        {
            memset(buf[i], 0, sizeof(buf[i]));
        }
        sprintf(buf[0], "%d", bzwd);
        sprintf(buf[1], "%d", bzsd);
        sprintf(buf[2], "%d", bzgz);
        sprintf(buf[3], "%d", bzqt);
        Display_characterX(750, 75, buf[0], 0x000000, 1);  // 温度
        Display_characterX(750, 100, buf[1], 0x000000, 1); // 湿度
        Display_characterX(750, 125, buf[2], 0x000000, 1); // 光照
        Display_characterX(750, 150, buf[3], 0x000000, 1); // 气体
    }
}
void wl_cmd(lcd *s)
{
    char buf[][8] = {"/13.bmp", "/12.bmp"};
    bmp_display(s, 600, 35, buf[!network_control]);
}